/// @tags: Kruskal
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

namespace BlueQuantum {

typedef long long LL;
typedef pair<int, int> pii;

int const N = 1e5 + 5, K = 3e5 + 5, W = 31;
int fa[N * 2];
LL ans[K], cnt[K];
vector<pii> A, B, e[W];

int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }

inline void solve(int n, int m, int w) {
  memset(cnt, 0, sizeof(LL) * (m + 1));
  for (int i = 1; i <= n * 2; ++i) fa[i] = i;
  for (int i = 1; i <= w; ++i)
    for (vector<pii>::iterator j = e[i].begin(), jlim = e[i].end(); j != jlim; ++j) {
      int x = find(j->first), y = find(j->second);
      if (x == y) continue;
      if (x > y) swap(x, y);
      fa[x] = y, cnt[1]++;
      if (x > n && y > n) A.push_back(make_pair(x - n, y - n));
    }
  for (int i = 2; i <= m && !A.empty(); ++i) {
    B.clear();
    for (vector<pii>::iterator j = A.begin(), jlim = A.end(); j != jlim; ++j) {
      int x = find(j->first), y = find(j->second);
      if (x == y) {
        cnt[i]--;
        continue;
      }
      if (x > y) swap(x, y);
      fa[x] = y;
      if (x > n && y > n) B.push_back(make_pair(x - n, y - n));
    }
    A.swap(B);
  }
  for (int i = 2; i <= m; ++i) cnt[i] += cnt[i - 1];
  for (int i = 2; i <= m; ++i) cnt[i] += cnt[i - 1];
  for (int i = 1; i <= m; ++i) ans[i] += n * (i + 1LL) - cnt[i] - 1;
}

inline int main() {
  int n, k, m;
  cin >> n >> k >> m;
  for (int i = 1, x, y, w; i <= m; ++i)
    cin >> x >> y >> w, y += n, e[w].push_back(make_pair(x, y));
  for (int i = 0; i < W; ++i) solve(n, k, i);
  for (int i = 1; i <= k; ++i) cout << ans[i] << '\n';
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("daisiki.in", "r", stdin);
  freopen("daisiki.out", "w", stdout);
#endif
#endif

  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
